GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#2843)
by Brendan
04:11
created

$.fn.symphonyTimeAgo   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 29
rs 6.7272
1
/**
2
 * @package assets
3
 */
4
5
(function($, Symphony) {
6
7
	/**
8
	 * Convert absolute to relative dates.
9
	 *
10
	 * @name $.symphonyTimeAgo
11
	 * @class
12
	 *
13
	 * @param {Object} options An object specifying containing the attributes specified below
14
	 * @param {String} [options.items='time'] Selector to find the absolute date
15
	 * @param {String} [options.timestamp='utc'] Attribute of `object.items` representing the timestamp of the given date
16
	 * @param {Integer} [options.max=0] Plugin will disable when the minutes exceed this value. By default this behaviour is off.
17
	 *
18
	 * @example
19
20
			$('.notifier').symphonyTimeAgo();
21
	 */
22
	$.fn.symphonyTimeAgo = function(options) {
23
		var objects = this,
24
			settings = {
25
				items: 'time',
26
				timestamp: 'utc',
27
				max: 0
28
			};
29
30
		$.extend(settings, options);
31
32
	/*-----------------------------------------------------------------------*/
33
34
		Symphony.Language.add({
35
			'just now': false,
36
			'a minute ago': false,
37
			'{$minutes} minutes ago': false,
38
			'about 1 hour ago': false,
39
			'about {$hours} hours ago': false
40
		});
41
42
	/*-------------------------------------------------------------------------
43
		Functions
44
	-------------------------------------------------------------------------*/
45
46
		function parse(item) {
47
			var timestamp = item.data('timestamp'),
48
				datetime;
49
50
			// Fetch stored timestamp
51
			if($.isNumeric(timestamp)) {
52
				return timestamp;
53
			}
54
55
			// Parse date
56
			else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
57
				datetime = item.attr(settings.timestamp);
58
59
				// Defined date and time
60
				if(datetime) {
61
					// Datetime will be in seconds since Epoch, JS requires
62
					// millseconds, so multiply by 1000.
63
					timestamp = new Date(datetime * 1000);
64
				}
65
66
				// Undefined date and time
67
				else {
68
					timestamp = new Date().getTime();
69
				}
70
71
				// Store and return timestamp
72
				item.data('timestamp', timestamp);
73
				return timestamp;
74
			}
75
		}
76
77
		function say(from, to) {
78
79
			// Calculate time difference
80
			var distance = to - from,
81
82
			// Convert time to minutes
83
			time = Math.floor(distance / 60000);
84
85
			// Return relative date based on passed time
86
			if(time < 1) {
87
				return Symphony.Language.get('just now');
88
			}
89
			if(time < 2) {
90
				return Symphony.Language.get('a minute ago');
91
			}
92
			if(time < 45) {
93
				return Symphony.Language.get('{$minutes} minutes ago', {
94
					'minutes': time
95
				});
96
			}
97
			if(time < 90) {
98
				return Symphony.Language.get('about 1 hour ago');
99
			}
100
			else if (!settings.max || time < settings.max) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if !settings.max || time < settings.max is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
101
				return Symphony.Language.get('about {$hours} hours ago', {
102
					'hours': Math.floor(time / 60)
103
				});
104
			}
105
		}
106
107
	/*-------------------------------------------------------------------------
108
		Initialisation
109
	-------------------------------------------------------------------------*/
110
111
		objects.find(settings.items).each(function timeago() {
112
			var item = $(this),
113
				from = parse(item),
114
				to = new Date(),
115
				rel = say(from, to);
116
117
			// Set relative time
118
			if (rel) {
119
				item.text(rel);
120
			}
121
		});
122
123
	/*-----------------------------------------------------------------------*/
124
125
		return objects;
126
	};
127
128
})(window.jQuery, window.Symphony);
129